package com.example.userservice.novel.controller;

import com.example.userservice.novel.entity.NovelGenerateRequest;
import com.example.userservice.novel.entity.NovelGenerateResponse;
import com.example.userservice.novel.entity.NovelStructure;
import com.example.userservice.novel.service.SimpleNovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/novel")
//@Slf4j
public class NovelController {

    @Autowired
    private SimpleNovelService simpleNovelService;

    /**
     * 生成小说结构
     */
    @PostMapping("/generate")
    public NovelGenerateResponse generateNovel(@RequestBody NovelGenerateRequest request) {
//        log.info("收到生成小说结构请求：{}", request.getNovelName());
        System.out.println("收到生成小说结构请求：" + request.getNovelName());

        long startTime = System.currentTimeMillis();
        try {
            NovelStructure structure = simpleNovelService.generateNovelStructure(request.getNovelName());
            long generateTime = System.currentTimeMillis() - startTime;
//            log.info("生成小说结构成功，耗时：{}ms", generateTime);
            System.out.println("生成小说结构成功，耗时：" + generateTime);
            return NovelGenerateResponse.success(structure, generateTime);
        } catch (Exception e) {
//            log.error("生成小说结构失败：{}", e.getMessage());
            System.out.println("生成小说结构失败," + e.getMessage());
            return NovelGenerateResponse.error(e.getMessage());
        }
    }

    /**
     * 逐步生成小说结构
     */
    @PostMapping("/generate/step")
    public NovelStructure generateStepByStep(@RequestBody NovelGenerateRequest request) {
//        log.info("收到逐步生成小说结构请求：{}", request.getNovelName());
        System.out.println("收到逐步生成小说结构请求：" + request.getNovelName());
        long startTime = System.currentTimeMillis();
        try {
            NovelStructure structure = simpleNovelService.generateStepByStep(request.getNovelName());
            long generateTime = System.currentTimeMillis() - startTime;
//            log.info("逐步生成小说结构成功，耗时：{}ms", generateTime);
            System.out.println("逐步生成小说结构成功，耗时：" + generateTime);
            return structure;
        } catch (Exception e) {
//            log.error("逐步生成小说结构失败：{}", e.getMessage());
            System.out.println("逐步生成小说结构失败," + e.getMessage());
            return null;
        }
    }

    /**
     * 快速生成接口
     */
    @GetMapping("/generate/quick")
    public NovelGenerateResponse quickGenerate(@RequestParam String novelName) {
        NovelGenerateRequest request = new NovelGenerateRequest(novelName);
        return generateNovel(request);
    }

    /**
     * 健康检查
     */
    @GetMapping("/health")
    public String healthCheck() {
        return "OK";
    }

}
